package com.scalar.chiptrack.operations.dataentry.lotsplit.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.lotsplit.forms.LotSplitForm;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 24, 2006
 * Time: 12:13:56 AM
 */
public class LotSplitLoadPartNumbersCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/lotSplit.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        LotSplitForm lotSplitForm = ( LotSplitForm ) form;

        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();

        String action   = request.getParameter("action");
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        String psTypeName = NewProductDAO.getBaseProcessName( null, lotSplitForm.getProcessType() );
        lotSplitForm.setBaseprocessType( psTypeName );
        ArrayList vendorList = new ArrayList();
       try
       {    if ( psTypeName.equalsIgnoreCase("MARK & PACK") )
            {
                 psTypeName = "Mark &amp; Pack";
            }
           if ( psTypeName.equalsIgnoreCase( LOCATION_TYPE_FAB ) || psTypeName.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || psTypeName.equalsIgnoreCase( LOCATION_TYPE_SORT ) /*|| psTypeName.equalsIgnoreCase(LOCATION_TYPE_DIE_BANK )*/ )
            {
                 request.setAttribute("showWaferIds", "TRUE");
            }
            else
            {
                request.setAttribute("showWaferIds", "FALSE");
            }
            ArrayList runningHoldLotsList =  DataEntryDAO.getLotInfoByPsType( "ALL", psTypeName, lotSplitForm.getProcessType(), Integer.parseInt(lotSplitForm.getProcessStepId()), Integer.parseInt(lotSplitForm.getLocationId() ), Integer.parseInt( lotSplitForm.getLotId() ) );
           if ( user.getVendorId() > 0 )
           {
                request.setAttribute("processTypeList", DataEntryDAO.loadExternalVendorProcessList(null, user.getVendorId() ) );
                lotSplitForm.setLocationId("0");
                if ( Integer.parseInt( lotSplitForm.getLocationId() ) == 0 )
                {
                    vendorList  = VendorDAO.getInventoryLocationsByProcessByUserType( null, psTypeName, user.getVendorId() );
                    if ( vendorList != null && vendorList.size() > 0 )
                    {
                        lotSplitForm.setLocationId( ( (LabelValueBean)vendorList.get(0) ).getValue() );
                    }
                    else
                    {
                        lotSplitForm.setLocationId( "-1");
                    }
                }

                //request.setAttribute("splitLotNoList", DataEntryDAO.getLotInfoByPsType( "ALL", psTypeName, lotSplitForm.getProcessType(), 0, Integer.parseInt(lotSplitForm.getLocationId() ) , 0 ) );
                request.setAttribute("splitLotNoList", runningHoldLotsList );
           }
           else
           {
                //request.setAttribute("processTypeList", DataEntryDAO.loadProcessList(null, "all" ) );
                request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
                //request.setAttribute("splitLotNoList", DataEntryDAO.getLotInfoByPsType( "ALL", psTypeName, lotSplitForm.getProcessType(), 0, 0, 0 ) );
               request.setAttribute("splitLotNoList", runningHoldLotsList );
           }



           if ( runningHoldLotsList == null || runningHoldLotsList.size() == 0 )
           {
                info.put("No "+lotSplitForm.getProcessType()+" lots available" , null );
           }

           lotSplitForm.setAllwipLotsList( runningHoldLotsList );

           request.setAttribute("partNumbersList", NewProductDAO.loadPartNumbers( null, 0, lotSplitForm.getProcessType(), true, true, true, 0 ) );
           request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserType( null, psTypeName, user.getVendorId() ) );

           request.setAttribute("action", action);
           request.setAttribute("psType", lotSplitForm.getProcessType() );
       }
       catch( Exception e)
       {
           if ( user.getVendorId() > 0 )
           {
                request.setAttribute("processTypeList", DataEntryDAO.loadExternalVendorProcessList(null, user.getVendorId() ) );
           }
           else
           {
               //request.setAttribute("processTypeList", DataEntryDAO.loadProcessList(null, "all" ) );
               request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
           }
          e.printStackTrace();
          errors.put("Could not load lots for Split", null); 
       }
       return mapping.findForward( "lotSplit" );
    }
}
